/*
 * Copyright (C) 2010-2011 sunjumper@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package mf.demo.client;

import java.io.IOException;
import java.io.InputStream;
import mf.demo.BaseUtil;
import mf.demo.User;
import mf.mfrpc.InvokeException;
import mf.mfrpc.client.RPCHttpClient;
import mf.mfrpc.serialize.DataSource;
import mf.mfrpc.serialize.DataSourceIterator;
import mf.mfrpc.serialize.FileDataSource;
import mf.mfrpc.serialize.FileDataSourceIterator;
import org.apache.commons.fileupload.util.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @see mf.demo.server.RPCServerAction
 */
public class RPCClient {

    private static final String PREFIX = "http://localhost:8080/MFrpc/rpc";

    private static final String testException = PREFIX + "/testException.rpc";

    private static final String testString = PREFIX + "/testString.rpc";

    private static final String testObject = PREFIX + "/testObject.rpc";

    private static final String testBean = PREFIX + "/testBean.rpc";

    private static final String testInputStream = PREFIX + "/testInputStream.rpc";

    private static final String testDataSource = PREFIX + "/testDataSource.rpc";

    private static final String testUploadDataSource = PREFIX + "/testUploadDataSource.rpc";

    private static final String testUploadDataSources = PREFIX + "/testUploadDataSources.rpc";

    //log
    private static final Logger LOG = LoggerFactory.getLogger(RPCClient.class);
    //

    public static void testException() throws InvokeException {
        RPCHttpClient<String> c = BaseUtil.getRPCHttpClient(testException);
        String res = c.invoke();
        LOG.info("res : [{}]", res);
    }
    //

    public static void testString() throws InvokeException {
        RPCHttpClient<String> c = BaseUtil.getRPCHttpClient(testString);
        String res = c.invoke("test value");
        LOG.info("res : [{}]", res);
    }

    //
    public static void testObject() throws InvokeException {
        RPCHttpClient c = BaseUtil.getRPCHttpClient(testObject);
        Object res = c.invoke();
        LOG.info("res type : [{}]", res.getClass());
        LOG.info("res : [{}]", res);
    }

    //
    public static void testBean() throws InvokeException {
        RPCHttpClient c = BaseUtil.getRPCHttpClient(testBean);
        Object res = c.invoke("root", "123456");
        LOG.info("res type : [{}]", res.getClass());
        assert res instanceof User;
        if (res instanceof User) {
            User u = (User) res;
            LOG.info("res : [{}]", u);
        }
    }

    //
    public static void testInputStream() throws IOException, InvokeException {
        RPCHttpClient<InputStream> c = BaseUtil.getRPCHttpClient(testInputStream);
        Object res = c.invoke(BaseUtil.getTestFile1().getPath());
        LOG.info("res type : [{}]", res.getClass());
        if (res instanceof InputStream) {
            InputStream is = (InputStream) res;
            LOG.info(Streams.asString(is, "utf-8"));
        }
    }

    //
    public static void testDataSource() throws IOException, InvokeException {
        RPCHttpClient c = BaseUtil.getRPCHttpClient(testDataSource);
        Object res = c.invoke(BaseUtil.getTestFile2().getPath());
        LOG.info("res type : [{}]", res.getClass());
        if (res instanceof DataSource) {
            DataSource ds = (DataSource) res;
            LOG.info("{},{},{}", new Object[]{ds.getName(), ds.getSize(), ds.getLastModified()});
            LOG.info(Streams.asString(ds.getInputStream(), "utf-8"));
        }
    }

    //
    public static void testUploadDataSource() throws InvokeException {
        RPCHttpClient<String> c = BaseUtil.getRPCHttpClient(testUploadDataSource);
        FileDataSource fds = new FileDataSource(BaseUtil.getTestFile1());
        Object res = c.invoke(fds);
        LOG.info("res : [{}]", res);
    }

    //
    public static void testUploadDataSources() throws InvokeException {
        RPCHttpClient<String> c = BaseUtil.getRPCHttpClient(testUploadDataSources);
        FileDataSource fds1 = new FileDataSource(BaseUtil.getTestFile1());
        FileDataSource fds2 = new FileDataSource(BaseUtil.getTestFile2());
        DataSourceIterator di = new FileDataSourceIterator(fds1, fds2);
        Object res = c.invoke(di);
        LOG.info("res : [{}]", res);
    }

    public static void main(String[] args) throws IOException, InvokeException {

        testException();
        System.out.println("----------------------------------------");
        testString();
        System.out.println("----------------------------------------");
        testObject();
        System.out.println("----------------------------------------");
        testBean();
        System.out.println("----------------------------------------");
        testInputStream();
        System.out.println("----------------------------------------");
        testDataSource();
        System.out.println("----------------------------------------");
        testUploadDataSource();
        System.out.println("----------------------------------------");
        testUploadDataSources();
        System.out.println("----------------------------------------");

    }
}
